<template>
  <div class="building-container">
    <!-- 添加区域 -->
    <div class="addcontainer">
      <el-button type="primary" round style="height: auto;" @click="open">添加区域</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style=" width: 100%">
        <!-- 序号列 -->
        <el-table-column type="index" label="序号" />
        <!-- 楼宇名称列 -->
        <el-table-column label="区域名称" prop="areaName" />
        <!-- 层数列 -->
        <el-table-column label="车位数(个)" prop="spaceNumber" />
        <!-- 在管面积列 -->
        <el-table-column label="面积(m²)" prop="areaProportion" />
        <!-- 关联计费规则列 -->
        <el-table-column label="关联计费规则" prop="ruleName" />
        <!-- 备注列 -->
        <el-table-column label="备注" prop="remark" />
        <!-- 操作列 -->
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="page-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.page"
        :page-sizes="[2, 4, 6, 8, 10]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 编辑/添加区域对话框 -->
    <el-dialog :visible="flag" @close="closeDialog" :title="form.id ? '编辑区域' : '新增区域'">
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 区域名称输入框 -->
        <el-form-item label="区域名称" prop="areaName">
          <el-input v-model="form.areaName"></el-input>
        </el-form-item>
        <!-- 车位数输入框 -->
        <el-form-item label="车位数(个)" prop="spaceNumber">
          <el-input v-model="form.spaceNumber"></el-input>
        </el-form-item>
        <!-- 面积输入框 -->
        <el-form-item label="面积(m²)" prop="areaProportion">
          <el-input v-model="form.areaProportion"></el-input>
        </el-form-item>
        <!-- 关联计费规则选择框 -->
        <el-form-item label="关联计费规则" prop="ruleId">
          <el-select v-model="form.ruleId">
            <el-option v-for="item in drop" :key="item.ruleId" :label="item.ruleName"
              :value="item.ruleId" />
          </el-select>
        </el-form-item>
        <!-- 备注输入框 -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :rows="4"></el-input>
        </el-form-item>
        <!-- 确认和取消按钮 -->
        <el-form-item>
          <el-button type="primary" round @click="confirm">确定</el-button>
          <el-button type="success" round @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { getAreaListAPI } from '@/api/area'
import { delAreaAPI } from '@/api/area'
import { addAreaAPI } from '@/api//area'
import { editAreaAPI } from '@/api/area'
import { getDropListAPI } from '@/api/area'

export default {
  name: 'Area',

  data() {
    return {
      // 控制编辑/添加楼宇对话框的显示隐藏
      flag: false,
      // 区域列表数据
      list: [],
      // 总数据量
      total: 0,
      drop: [],
      // 查询参数
      params: {
        page: 1,
        pageSize: 6,
        name: '',
      },

      // 编辑/添加区域的表单数据
      form: {
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        ruleId: '',

        remark: ''
      },
      // 表单校验规则
      rules: {
        areaName: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        spaceNumber: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        areaProportion: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        ruleId: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    // 页面加载时调用获取列表数据的方法
    this.getList()
  },
  methods: {

    getDropList() {
      getDropListAPI().then((res) => {
        this.drop = res.data
      })
    },

    // 编辑区域信息
    edit(row) {
      console.log(row)
      // 打开弹窗
      this.flag = true

      // 将要编辑的区域信息回显到表单中
      this.form.areaName = row.areaName
      this.form.spaceNumber = row.spaceNumber
      this.form.areaProportion = row.areaProportion
      this.form.ruleId = row.ruleId
      this.form.remark = row.remark

      this.form.id = row.id//将来编辑需要的id
    },

    // 点击确认按钮提交表单数据
    confirm() {
      // 校验表单数据
      this.$refs.form.validate((ok) => {
        if (ok) {
          if (this.form.id) {
            console.log('编辑')
            // 调用编辑区域信息的 API
            editAreaAPI(this.form).then(() => {
              this.$message.success('编辑成功！')
              // 刷新列表数据
              this.getList()
              // 关闭对话框
              this.flag = false
              // 编辑完成以后清空数据，删除id字段
              this.form = {
                areaName: '',
                spaceNumber: '',
                areaProportion: '',
                ruleId: '',
                remark: ''
              }
            }).catch(() => {
              // this.$message.error(error.response.data.msg)
              this.$message.error('编辑失败！')
            })
          } else {
            // 调用添加区域信息的 API
            addAreaAPI(this.form).then(() => {
              this.$message.success('添加成功！')
              // 刷新列表数据
              this.getList()
              // 关闭对话框
              this.flag = false
              // 清空表单数据
              this.form = {
                areaName: '',
                spaceNumber: '',
                areaProportion: '',
                ruleId: '',
                remark: ''
              }
            }).catch(() => {
              // this.$message.error(error.response.data.msg)
              this.$message.error('添加失败！')
            })
          }
        }
      })
    },

    // 关闭编辑/添加楼宇对话框
    closeDialog() {
      this.flag = false
      // 清空表单数据
      this.form = {
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        ruleId: '',
        remark: ''
      }
    },

    // 获取楼宇列表数据
    getList() {
      getAreaListAPI(this.params).then((res) => {
        this.list = res.data.rows,
          this.total = res.data.total
      })
    },

    // 点击添加楼宇按钮，打开编辑/添加楼宇对话框
    open() {
      this.flag = true
      this.getDropList()
    },
    // 分页相关方法
    handleSizeChange(pageSize) {
      this.params.page = 1,
        this.params.pageSize = pageSize,
        this.getList()
    },
    handleCurrentChange(page) {
      this.params.page = page
      this.getList()
    },

    // 删除楼宇信息
    del(id) {
      this.$confirm('你确定要删除吗？').then(() => {
        delAreaAPI(id).then(() => {
          // 刷新列表数据
          this.getList()
          this.$message.success('删除成功！')
        }).catch(() => {
          this.$message.error('删除失败')
        })
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
